/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.generator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author yunglu
 */
public class PieceList {

    public PieceList() {
    }

    public void setNumberSquare(int n) {
        if (numberSquare == n) {
            return; // no need to do anything
        }
        numberSquare = n;
        generatePieces();
        /* System.out.println("There are " + pieceList.size() +
                " pieces of " + n + " squares."); */
    }
    
    public List<BlokusPiece> getPieces() {
        return pieceList;
    }

    private void addPiece(List<boolean[][]> bList) {
        Iterator iter = bList.iterator();
        while (iter.hasNext()) {
            boolean[][] matrix = (boolean[][]) iter.next();
            BlokusPiece pieceToAdd = new BlokusPiece(matrix);
            Iterator pIter = pieceList.iterator();
            boolean equivalent = false;
            while ((equivalent == false) && (pIter.hasNext())) {
                BlokusPiece currPiece = (BlokusPiece) pIter.next();
                equivalent = currPiece.equivalent(pieceToAdd);
            }
            if (equivalent == false) {
                pieceList.add(pieceToAdd);
            }
        }
    }

    private void generatePieces() {
        List<List<Integer>> partitionList = Partition.partition(numberSquare);
        Iterator<List<Integer>> iter = partitionList.iterator();
        pieceList.clear(); // remove all existing pieces
        while (iter.hasNext()) {
            List<Integer> onePartition = (List<Integer>) iter.next();
            // warning: [unchecked] unchecked cast, I am not sure how to fix it
            List<boolean[][]> bList = BlokusPiece.createMatrix(onePartition);
            if (bList != null) {
                addPiece(bList);
            }
        }
    }
    private int numberSquare = 0;
    private List<BlokusPiece> pieceList = new ArrayList<BlokusPiece>();
}
